package com.panshi.controller;

import com.alibaba.fastjson.JSON;
import com.panshi.api.AnswerApi;
import com.panshi.api.UserApi;
import com.panshi.po.Answer;
import com.panshi.po.CommentList;
import com.panshi.po.RevertAll;
import com.panshi.qa.api.HasbeiQaApp;
import com.panshi.qa.api.vo.ItemQueryVo;
import com.panshi.qa.api.vo.QuestionResVO;
import com.panshi.utils.LoginVerifyShowPage;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class AnswerController {
    @Autowired
    AnswerApi answerApi;

    @Autowired
    HasbeiQaApp hasbeiQaApp;

    @Autowired
    UserApi userApi;

    //回答类型
    @RequestMapping("/answerType")
    public ModelAndView answerType(long id,HttpSession httpSession){

        log.info("类型id："+id);

        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> map = hasbeiQaApp.question(id).getData();

        /*类型id*/
        modelAndView.addObject("typeId",id);

        Object bigList = map.get("bigList");
        QuestionResVO resVO = JSON.parseObject(bigList.toString(), QuestionResVO.class);

        /*排行榜*/
        modelAndView.addObject("orderList",resVO.getOrderList());

        /*问答列表*/
        modelAndView.addObject("questionList",resVO.getQuestionList());

        /*类型*/
        modelAndView.addObject("thTypeList",resVO.getThTypeList());

        //登录后展示头部栏
        modelAndView.addObject("userObject",new LoginVerifyShowPage().showPage(userApi,httpSession));

        modelAndView.setViewName("askAnswer");
        return modelAndView;
    }

    //问答
    @RequestMapping("/askAnswer")
    public ModelAndView askAnswer(HttpSession httpSession){
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> map = hasbeiQaApp.question().getData();

        /*类型id*/
        modelAndView.addObject("typeId",0);

        Object bigList = map.get("bigList");
        QuestionResVO resVO = JSON.parseObject(bigList.toString(), QuestionResVO.class);

        /*排行榜*/
        modelAndView.addObject("orderList",resVO.getOrderList());

        /*问答列表*/
        modelAndView.addObject("questionList",resVO.getQuestionList());

        /*类型*/
        modelAndView.addObject("thTypeList",resVO.getThTypeList());

        //登录后展示头部栏
        modelAndView.addObject("userObject",new LoginVerifyShowPage().showPage(userApi,httpSession));

        modelAndView.setViewName("askAnswer");
        return modelAndView;
    }

    //未回答（问答）
    //已回答（问答）
    @RequestMapping("/qa/index/item")
    public ModelAndView haveToAnswer(Integer type, Integer current, Integer replyStatus, Long cateId, HttpServletRequest httpServletRequest){
        ModelAndView modelAndView = new ModelAndView();

        ItemQueryVo itemQueryVo = new ItemQueryVo();
        itemQueryVo.setType(type);
        itemQueryVo.setCurrent(current);
        itemQueryVo.setReplyStatus(replyStatus);
        itemQueryVo.setCateId(cateId);

        Map<String, Object> map = hasbeiQaApp.itemQuery(itemQueryVo).getData();
        Object bigList = map.get("bigList");
        QuestionResVO resVO = JSON.parseObject(bigList.toString(), QuestionResVO.class);

        /*问答列表*/
        modelAndView.addObject("questionList",resVO.getQuestionList());

        modelAndView.setViewName("div");
        return modelAndView;
    }

    //问答详情
    @RequestMapping("/askAnswerDetail")
    @ResponseBody
    public ModelAndView askAnswerDetail(long id, HttpSession httpSession){

        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> map = hasbeiQaApp.detail(id).getData();

        Object bigList = map.get("bigList");
        QuestionResVO resVO = JSON.parseObject(bigList.toString(), QuestionResVO.class);

        //详情对象
        modelAndView.addObject("detaiObject",resVO.getQaQuestionAssociated());

        modelAndView.addObject("answerId",id);
        modelAndView.addObject("typeId",resVO.getQaQuestionAssociated().getTypeId());

        //相关问题排行榜
        modelAndView.addObject("orderList",resVO.getQaQuestionOrderList());
        //问题排行榜
        modelAndView.addObject("orderList1",resVO.getQaQuestionOrderList1());
        //类型
        modelAndView.addObject("thTypeList",resVO.getThTypeList());
        //访问时间
        modelAndView.addObject("time",new Date().getTime());


        QueryAnswerVO queryAnswerVO = new QueryAnswerVO();
        queryAnswerVO.setCurrentPage(0);
        queryAnswerVO.setPageSize(10);
        queryAnswerVO.setQuestionId(id);

        Result<List<Answer>> selectPage = answerApi.selectPage(queryAnswerVO);

        modelAndView.addObject("listResponseEntity",selectPage.getData());

        //登录后展示头部栏
        modelAndView.addObject("userObject",new LoginVerifyShowPage().showPage(userApi,httpSession));

        modelAndView.setViewName("askAnswerDetail");
        return modelAndView;
    }

    //发表回答
    @PostMapping("/qa/addReply")
    @ResponseBody
    public Result addReply(String content,Integer answerId,HttpSession httpSession){
        Integer userId=(Integer)httpSession.getAttribute(httpSession.getId());

        SaveAnswerVO saveAnswerVO = new SaveAnswerVO();
        saveAnswerVO.setContent(content);
        saveAnswerVO.setUserId(userId);
        saveAnswerVO.setQuestionId(answerId);
        //saveAnswerVO.setPhotoPath();

        Result result = answerApi.save(saveAnswerVO);
        return result;
    }

    //点赞，点踩
    @PostMapping("/qa/reply/option")
    @ResponseBody
    public Result clickOption(Long qaReplyId, Integer type,HttpSession httpSession){

        Integer userId=(Integer)httpSession.getAttribute(httpSession.getId());

        SaveOptionVO saveOptionVO = new SaveOptionVO();
        saveOptionVO.setType(20);
        saveOptionVO.setPattern(type);
        saveOptionVO.setUserId(userId.longValue());
        saveOptionVO.setTId(qaReplyId);

        Result result = answerApi.option(saveOptionVO);
        return result;
    }

    //新增评论
    @PostMapping("/qa/reply/addComment")
    @ResponseBody
    public Result addComment(Long qaReplyId,String content,Long pid,HttpSession httpSession){

        Integer userId=(Integer)httpSession.getAttribute(httpSession.getId());

        SaveCommentVO saveCommentVO = new SaveCommentVO();
        saveCommentVO.setParentId(pid);
        saveCommentVO.setAnswerId(qaReplyId);
        saveCommentVO.setContent(content);
        saveCommentVO.setUserId(userId.longValue());//模拟用户

        Result result = answerApi.saveComment(saveCommentVO);
        return result;
    }

    //查询评论
    @PostMapping("qa/reply/commentlist")
    @ResponseBody
    public ModelAndView commentlist(Integer current,Long pid,Long qaReplyId){

        ModelAndView modelAndView = new ModelAndView();

        QueryCommentVO queryCommentVO = new QueryCommentVO();
        queryCommentVO.setAnswerId(qaReplyId);
        queryCommentVO.setParentId(pid);
        queryCommentVO.setCurrent(current);

        if (pid>=1){
            Result<List<RevertAll>> result = answerApi.revertAll(queryCommentVO);
            if (result.getData().get(0).getId()!=null){
                modelAndView.addObject("data",result.getData());
                modelAndView.addObject("fu","");
                modelAndView.setViewName("comment-list(1)");
            }
            modelAndView.setViewName("comment-list(1)");
        }else{
            Result<List<CommentList>> result = answerApi.commentlist(queryCommentVO);
            modelAndView.addObject("data",result.getData());
            modelAndView.setViewName("comment-list");
        }
        return modelAndView;
    }

    //点赞
    @PostMapping("qa/reply/commentOption")
    @ResponseBody
    public Result commentOption(Long commentId,Integer type,HttpSession httpSession){
        Integer userId=(Integer)httpSession.getAttribute(httpSession.getId());

        SaveOptionVO saveOptionVO = new SaveOptionVO();
        saveOptionVO.setType(30);
        saveOptionVO.setPattern(type);
        saveOptionVO.setUserId(userId.longValue());
        saveOptionVO.setTId(commentId);

        Result result = answerApi.option(saveOptionVO);
        return result;
    }
}
